#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
using namespace std;
// #define self_protocol 1

// 模拟定协议

const string sepa = " ";
const string line_break = "\n";

// 添加报头数据和解报头
void Encode(string &mes)
{
    int len = mes.size();
    string ret = to_string(len) + line_break + mes + line_break;
    mes = ret;
}

bool Decode(string &package, string &ret)
{
    // 格式："len\n数据信息\n"
    // 先判断收到的数据中是否可以提炼出一次完整的数据请求
    int len = package.size();
    int pos = package.find(line_break, 0); // 指向第一个换行符
    if (pos == string::npos)
        return false;

    int data_size = stoi(package.substr(0, pos));
    int sz = pos + line_break.size() + data_size + line_break.size();
    if (sz > len)
        return false;

    // 解包后的数据
    ret = package.substr(pos + line_break.size(), data_size);
    // 去掉被读走的数据
    package = package.substr(sz);

    return true;
}

class Request
{
    friend class Cal;

public:
    Request() {}
    Request(int x, int y, char op)
        : _x(x), _y(y), _oper(op)
    {
    }

    void info()
    {
        cout << _x << ' ' << _oper << ' ' << _y << " = ?" << endl;
    }

    void Serialize(string &out) // 序列化
    {
#ifdef self_protocol
        out = to_string(_x) + sepa + _oper + sepa + to_string(_y);
#else
        Json::Value ser;
        Json::FastWriter write;
        ser["x"] = _x;
        ser["oper"] = _oper;
        ser["y"] = _y;
        out = write.write(ser);
#endif
    }

    //"x + y"
    void Deserialize(const string s) // 反序列化
    {
#ifdef self_protocol

        int begin = 0;
        int end = s.find(sepa, begin);
        _x = stoi(s.substr(begin, end - begin));
        begin = end + sepa.size(); // 加的1其实就是' '的长度

        end = s.find(sepa, begin);
        _oper = s.substr(begin, end - begin)[0];
        begin = end + sepa.size();

        _y = stoi(s.substr(begin));
#else
        Json::Value result;
        Json::Reader read;
        read.parse(s, result);
        _x = result["x"].asInt();
        _y = result["y"].asInt();
        _oper = result["oper"].asInt();
#endif
    }

private:
    int _x;
    int _y;
    char _oper;
};

class Response
{
public:
    Response()
    {
    }
    Response(int re, string ret_info)
        : _result(re), _ret_info(ret_info)
    {
    }
    void Info()
    {
        cout << "result = " << _result << " (" << _ret_info << ')' << endl;
    }

    void Serialize(string &out) // 序列化
    {
#ifdef self_protocol

        out = to_string(_result) + sepa + _ret_info;
#else

        Json::Value ser;
        Json::FastWriter write;
        ser["result"] = _result;
        ser["ret_info"] = _ret_info;
        out = write.write(ser);
#endif
    }

    //"_result _ret_info"
    void Deserialize(const string s) // 反序列化
    {
#ifdef self_protocol
        int begin = 0;
        int end = s.find(sepa, begin);
        _result = stoi(s.substr(begin, end - begin));
        begin = end + sepa.size(); // 加的1其实就是分隔符的长度

        _ret_info = s.substr(begin);
#else

        Json::Value result;
        Json::Reader read;
        read.parse(s, result);
        _result = result["result"].asInt();
        _ret_info = result["ret_info"].asString();
#endif
    }

private:
    int _result;      // 保存结果
    string _ret_info; // 结果信息
};